package com.dzu.easy;

import java.util.Arrays;

/**
 * @author ZhaoDong
 * @date 2022/12/23 10:46
 * @description 682. 棒球比赛
 */
public class Test682 {

    public static void main(String[] args) {
        System.out.println(calPoints(new String[]{"5", "2", "C", "D", "+"}));
    }

    public static int calPoints(String[] operations) {

        int[] arr = new int[operations.length];
        int index = 0;
        for (int i = 0; i < operations.length; i++) {
            String str = operations[i];
            switch (str) {
                case "+":
                    if (index > 1) {
                        arr[index] = arr[index - 1] + arr[index - 2];
                        index++;
                    }
                    break;
                case "D":
                    if (index > 0) {
                       arr[index] =  arr[index - 1] * 2;
                        index++;
                    }
                    break;
                case "C":
                    if (index > 0) {
                        arr[--index] = 0;
                    }
                    break;

                default:
                    arr[index++] = Integer.parseInt(str);
            }
        }

        return Arrays.stream(arr).reduce(0, Integer::sum);
    }
}
